﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace CommonClass
{
    /// <summary>
    /// 重装封装为统一的对象，便于树状控件上的统一展示
    /// </summary>
    public class TreeGroupModel : INotifyPropertyChanged, ITreeGroup
    {
        /// <summary>
        /// 非叶子结点类，自身可迭代多次
        /// </summary>
        /// <param name="name"></param>
        /// <param name="leaves"></param>
        public TreeGroupModel(string name, ObservableCollection<ITreeGroup> leaves)
        {
            _name = name;
            _leaves = leaves;
            _isSelected = false; // 默认未选中结点
            _isExpanded = true; // 默认展开结点
            _isVisible = Visibility.Collapsed; // 不显示 Checkbox
        }

        #region ITreeGroup 成员

        private bool? _isSelected;
        public bool? IsSelected
        {
            get
            {
                return _isSelected;
            }
            set
            {
                if (value == _isSelected) return;
                _isSelected = value;
                RaisePropertyChanged("IsSelected");
            }
        }

        private bool _isExpanded;
        public bool IsExpanded
        {
            get 
            {
                return _isExpanded; 
            }
            set
            {
                if (value == _isExpanded) return;
                _isExpanded = value;
                RaisePropertyChanged("IsExpanded");
            }
        }

        private Visibility _isVisible;
        public Visibility IsVisible
        {
            get { return _isVisible; }
            set
            {
                if (value == _isVisible) return;
                _isVisible = value;
                RaisePropertyChanged("IsVisible");
            }
        }

        private object _arg;
        public object Arg
        {
            get
            {
                return _arg;
            }
            set
            {
                if (value == _arg) return;
                _arg = value;
                RaisePropertyChanged("Arg");
            }
        }

        private ITreeGroup _parent;
        public ITreeGroup Parent
        {
            get
            {
                return _parent;
            }
            set
            {
                //if (value == _parent) return;
                _parent = value;
                RaisePropertyChanged("Parent");
            }
        }

        private string _tooltipText;
        public string TooltipText
        {
            get
            {
                return _tooltipText;
            }
            set
            {
                if (value == _tooltipText) return;
                _tooltipText = value;
                RaisePropertyChanged("TooltipText");
            }
        }

        private int _level;
        public int Level
        {
            get
            {
                return _level;
            }
            set
            {
                if (value == _level) return;
                _level = value;
                RaisePropertyChanged("Level");
            }
        }



        private string _name;
        public string Name
        {
            get
            {
                return _name;
            }
        }

        private ObservableCollection<ITreeGroup> _leaves;
        public ObservableCollection<ITreeGroup> Leaves
        {
            get
            {
                return _leaves;
            }
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }


    #region Interface

    /// <summary>
    /// 非叶子结点【接口】
    /// </summary>
    public interface ITreeGroup
    {
        /// <summary>
        /// 结点是否选中
        /// </summary>
        bool? IsSelected { get; set; }

        /// <summary>
        /// 结点是否展开
        /// </summary>
        bool IsExpanded { get; set; }

        /// <summary>
        /// Checkbox 是否可见
        /// </summary>
        Visibility IsVisible { get; set; }

        /// <summary>
        /// 其它附加参数
        /// </summary>
        object Arg { get; set; }

        /// <summary>
        /// 父结点
        /// </summary>
        ITreeGroup Parent { get; set; }

        /// <summary>
        /// Tooltip
        /// </summary>
        string TooltipText { get; set; }

        /// <summary>
        /// 结点层数
        /// </summary>
        int Level { get; set; }



        /// <summary>
        /// 结点名称
        /// </summary>
        string Name { get; }

        /// <summary>
        /// 非叶子列表
        /// </summary>
        ObservableCollection<ITreeGroup> Leaves { get; }
    }

    #endregion
}
